package com.tth.learn.addproperties.java;

import org.junit.Test;

import java.util.HashSet;
import java.util.Objects;

/**
 * @Author tth
 * @Date 2022/12/13 10:54
 */
public class SetTest {
    @Test
    public void test1() {
        HashSet set = new HashSet();
        Teacher t1 = new Teacher(1001, "AA");
        Teacher t2 = new Teacher(1002, "BB");

        set.add(t1);
        set.add(t2);
        System.out.println(set);

        t1.setName("CC");
        set.remove(t1);
        System.out.println(set);

        set.add(new Teacher(1001, "CC"));
        System.out.println(set);

        set.add(new Teacher(1001, "AA"));
        System.out.println(set);
    }
}

class Teacher {
    private int id;
    private String name;


    public Teacher(int id, String name) {
        this.name = name;
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getid() {
        return id;
    }

    public void setid(int id) {
        this.id = id;
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Teacher teacher = (Teacher) o;
        return id == teacher.id &&
                Objects.equals(name, teacher.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, name);
    }

    @Override
    public String toString() {
        return "Teacher{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

